package com.edu.hre.struts.action.admin;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.DynamicConfig;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreConfig;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.struts.form.admin.ConfigurationForm;

public class ConfigurationUpdateAction extends DispatchAction {
	
	private static final Logger logger = Logger.getLogger(ConfigurationUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "adminConfigsUpdate";
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		ConfigurationForm beanForm = (ConfigurationForm) form;
		beanForm.setIsAddNew(true);
		
		if(request.getParameter(StringConstants.BTN_ADD) == null){
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		} else if(StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getKey()))){
			beanForm.setMessage("Key string cannot be blank!");
			return mapping.findForward(ACTION_NAME);
		} 
		HreConfig config = new HreConfig();
		config.setKey1(CommonUtils.normalizeString(beanForm.getKey()));
		config.setValue(beanForm.getValue());
		config.setComment(CommonUtils.normalizeString(beanForm.getComment()));
		config.setDateCreated(new Date());
		config.setDateModified(new Date());
		if(!baseService.save(config)){
			beanForm.setMessage("Key string alreadry exists!");
			return mapping.findForward(ACTION_NAME);
		}
		DynamicConfig.loadDynamicConfig();
		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		ConfigurationForm beanForm = (ConfigurationForm) form;
		beanForm.setIsAddNew(false);
		
		if(request.getParameter(StringConstants.BTN_EDIT) == null){
			int id = CommonUtils.getIntParameter("id", request);
			HreConfig tmp = baseService.getPojo(id, HreConfig.class);
			if (tmp == null) {
				beanForm.setMessage(StringConstants.BLANK);
				return mapping.findForward(ACTION_NAME);
			}
			beanForm.setId(id);
			beanForm.setKey(CommonUtils.normalizeString(tmp.getKey1()));
			beanForm.setValue(CommonUtils.normalizeString(tmp.getValue()));
			beanForm.setComment(CommonUtils.normalizeString(tmp.getComment()));
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}
		
		if(StringUtils.isBlank(CommonUtils.normalizeString(beanForm.getKey()))){
			beanForm.setMessage("Key string cannot be blank!");
			return mapping.findForward(ACTION_NAME);
		} 
		HreConfig tmp = baseService.getPojo(beanForm.getId(), HreConfig.class);
		if (tmp == null) {
			beanForm.setMessage(StringConstants.BLANK);
			return mapping.findForward(ACTION_NAME);
		}
		tmp.setId(beanForm.getId());
		tmp.setKey1(CommonUtils.normalizeString(beanForm.getKey()));
		tmp.setValue(CommonUtils.normalizeString(beanForm.getValue()));
		tmp.setComment(CommonUtils.normalizeString(beanForm.getComment()));
		tmp.setDateModified(new Date());

		baseService.saveOrUpdate(tmp);
		DynamicConfig.loadDynamicConfig();
		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}
}
